/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package TabelCreate;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 * @author BAGUS
 */
public class CreateTable {
    public static void main(String[] args) {
        String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:XE";
        String userid = "hr"; // gantikan dengan username anda
        String password = "hr"; // gantikan dengan password anda
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(password);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            statement.executeQuery("create table baranggudang (idbarang varchar2(4) constraint barang_pk primary key,"
                    + " namabarang varchar2(20) not null unique,"
                    + " jumlahbarang number(6) not null,"
                    + " hargabarang number(6) not null)");
            statement.executeQuery("create table usergudang (username varchar2(10) constraint usergudang_pk primary key,"//1
                    + " namauser varchar2(25) not null,"
                    + " alamatuser varchar2(50) not null,"
                    + " notelepon varchar2(15) not null,"
                    + " jenisuser varchar2(8) not null,"
                    + " password varchar2(10) not null)");
            statement.executeQuery("create table pemesanan (kodepemesanan varchar2(9) constraint pemesanan_pk primary key ,"//2
                    + " waktupemesanan varchar2(10) not null,"
                    + " jumlahbarang number(6) not null,"
                    + " idbarang varchar2(20) constraint pemesanan_fk1 references baranggudang(idbarang),"
                    + " username varchar2(6) constraint pemesanan_fk2 references usergudang(username),"
                    + " status varchar2(10) not null,"
                    + " jumlahbarangterkirim number(6) not null,"
                    + " jumlahbarangbelumterkirim number(6) not null)");
            statement.executeQuery("create table penerimaan (kodepenerimaan varchar2(9) constraint penerimaan_pk primary key ,"//2
                    + " waktupenerimaan varchar2(10) not null,"
                    + " jumlahbarang number(6) not null,"
                    + " hargabarang number(6) not null,"
                    + " idbarang varchar2(4) constraint penerimaan_fk1 references baranggudang(idbarang),"
                    + " username varchar2(10) constraint penerimaan_fk2 references usergudang(username))");            
            statement.executeQuery("create table pengiriman (kodepengiriman varchar2(9) constraint pengiriman_pk primary key ,"//3
                    + " waktupengiriman varchar2(10) not null,"
                    + " kodepemesanan constraint pengiriman_fk1 references pemesanan(kodepemesanan),"
                    + " username constraint pengiriman_fk2 references usergudang(username),"
                    + " jumlahbarang number(6) not null,"
                    + " idbarang varchar2(4) constraint pengiriman_fk3 references baranggudang(idbarang),"
                    + " hargabarang number(6) not null)");
            System.out.println("table created");
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
        }
    }
}
